<template>
    <view class="content">
        <u-cell-group>
            <u-swipe-action v-for="item in sourceList" :key="item.id" :autoClose="true">
                <u-swipe-action-item :options="options2" @click="changeOperation" :name="item.id">
                    <view class="swipe-action u-border-top u-border-bottom">
                        <view class="swipe-action__content">
                            <u-cell :title="item.name" center></u-cell>
                        </view>
                    </view>
                </u-swipe-action-item>
            </u-swipe-action>
        </u-cell-group>
        <u-button @click="toAdd" :customStyle="btnStyle" color="#19BC9C" type="success">添加</u-button>
        <u-modal :show="show" :title="title" :content='content' :showCancelButton="true" @confirm="confirm"
            @cancel="show = false"></u-modal>
        <u-toast ref="uToast"></u-toast>
    </view>
</template>

<script>
export default {
    data() {
        return {
            options2: [{
                text: '编辑',
                id: 'edit',
                style: {
                    backgroundColor: '#3c9cff'
                }
            }, {
                text: '删除',
                id: 'del',
                style: {
                    backgroundColor: '#f56c6c'
                }
            }],
            sourceList:[],
            sourceid:0,
            show:false,
            title:'删除',
            content:'是否确认删除该客户来源？',
            btnStyle: {
                width: '100%',
                position: 'fixed',
                bottom: '0px'
            },
            LoginAdmin:uni.getStorageSync('LoginAdmin') ? uni.getStorageSync('LoginAdmin') : {}
        }
    },
    methods: {
        async changeOperation(event)
        {
            // console.log(options);

            // console.log(this.options2[event.index].id);
            if(this.options2[event.index].id === 'del'){
                this.show = true;
                this.sourceid = event.name;
            }
            if(this.options2[event.index].id === 'edit'){
                this.$u.route({
                    url:'pages/manage/source/edit',
                    params:{
                            sourceid:event.name
                        }
                    });
            }
        },

        toAdd()
        {
            this.$u.route('pages/manage/source/add');
        },
        async confirm()
        {
            // console.log(1111);
            // show = true;
            console.log(this.sourceid);
            let data = {
                    adminid:this.LoginAdmin.id,
                    id:this.sourceid
            }
            let result = await this.$u.api.manage.SourceDel(data);
            if(result.code === 1)
                {
                    this.$refs.uToast.show({
                        type:'success',
                        message:result.msg,
                        complete:() => {
                            this.show = false;
                            this.getData();
                            return;
                        }
                    })
                }else{
                    this.$refs.uToast.show({
                        type:'error',
                        message:result.msg
                    });

                    return;
                }
        },
        async getData()
        {
            let result = await this.$u.api.manage.SourceIndex({adminid:this.LoginAdmin.id});

            if(result.code === 0)
            {
                this.$refs.uToast.show({
                    type:'error',
                    message:result.msg
                });

                return;
            }

            this.sourceList = result.data;
        }
    },
    onLoad()
    {
        console.log('onLoad');
    },
    onShow()
    {
        this.getData();
    },
    onReady()
    {
        console.log('onReady');
    }
}
</script>


<style>
</style>